package com.my.hotel.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.my.hotel.entity.Order;
import com.my.hotel.service.TbOderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/tbOrder")
public class TbOrderController {

    @Autowired
    private TbOderService tbOderService;

    @RequestMapping("/tbOrderPage")
    public String getPage(Model model, Integer pageNo, String order_num,String order_status,String user_id){
        model.addAttribute("name");
        if(pageNo==null||pageNo==0){
            pageNo=1;
        }
        if(order_num==null||order_num.equals("")){
            order_num="";
        }
        if(order_status==null||order_status.equals("")){
            order_status="";
        }

        if(user_id==null||user_id.equals("")){
            user_id="";
        }

        PageHelper.startPage(pageNo,10);
        List<Order> list =tbOderService.getList(order_num,order_status,user_id);
        PageInfo<Order> page = new PageInfo<Order>(list);
        if (order_status.equals("")){
            order_status="4";
        }

        model.addAttribute("pageNo",pageNo);
        model.addAttribute("page",page);
        model.addAttribute("name",order_num);
        model.addAttribute("order_status",order_status);
        model.addAttribute("name2",user_id);
        return "odr";
    }

    //取消
    @RequestMapping("/tbCal")
    public String tbCal(int id,Model model,String orderStatus){
        if(orderStatus.equals("0")){
            tbOderService.calOdr(id);
            return getPage(model);
        }else {
            return getPage(model);
        }
    }

    //预定
    @RequestMapping("/tbPre")
    public String tbPre(int id,Model model,String orderStatus){
        System.out.println(orderStatus);
        if(orderStatus.equals("1")){
            tbOderService.preOdr(id);
            return getPage(model);
        }else {
            return getPage(model);
        }
    }

    //入住
    @RequestMapping("/tbLive")
    public String tbLive(int id,Model model,String orderStatus){
        if(orderStatus.equals("0")){
            tbOderService.liveOdr(id);
            return getPage(model);
        }
        return getPage(model);
    }

    //退房
    @RequestMapping("/tbReturn")
    public String tbReturn(int id,Model model,String orderStatus){
        if(orderStatus.equals("2")){
            tbOderService.returnOdr(id);
            return getPage(model);
        }
        return getPage(model);
    }

    public String getPage(Model model){
        return getPage(model,1,"","","");
    }

}
